package sleuthkit.gui.actionlistener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;
import javax.swing.JOptionPane;

import org.apache.log4j.Logger;

import sleuthkit.gui.MainApplication;
import sleuthkit.util.CaseManagement;
import sleuthkit.util.StaticHelper;

public class BtnCreateActionListener implements ActionListener {
	private JComboBox caseJComboBox;
	private JComboBox investigatorJComboBox;
	private MainApplication parent;
	private Logger log = Logger.getLogger(BtnCreateActionListener.class);
	private boolean dontChangePane = false;

	public BtnCreateActionListener(JComboBox caseJComboBox,
			JComboBox investigatorJComboBox, MainApplication parent) {
		super();
		StaticHelper.configLogger();
		this.caseJComboBox = caseJComboBox;
		this.investigatorJComboBox = investigatorJComboBox;
		this.parent = parent;
	}
	
	@SuppressWarnings("unused")
	private BtnCreateActionListener() {
	}

	@SuppressWarnings("null")
	public void actionPerformed(ActionEvent e) {
		if (caseJComboBox.getSelectedIndex() > 0) {
			log.debug("Load the case " + caseJComboBox.getSelectedItem());
		} else if (caseJComboBox.getSelectedIndex() == 0) {
			log.debug("The case can't have an empty string as a name");
		} else {
			log.debug("create a new case");
			CaseManagement cm = CaseManagement.getInstance((String) caseJComboBox.getSelectedItem());
			cm.createCase();
			log.debug("add investigator to the JComboBox");
			String investigator = (String) investigatorJComboBox.getSelectedItem();
			log.debug("investigator = " + investigator);
			log.debug("investigatorJComboBox.getSelectedIndex() = " + investigatorJComboBox.getSelectedIndex());
			if (investigatorJComboBox.getSelectedIndex() == -1){
				
				if (investigator != null || !investigator.equalsIgnoreCase("")){
					investigatorJComboBox.addItem(investigator);
				} else {
					JOptionPane.showMessageDialog(null,
							"You have to choose an existing investigator"+"\n"+"or create a new one by write the name in the invesitgator box",
							"Error:No Investigor",
							JOptionPane.ERROR_MESSAGE);
					dontChangePane  = true;
				}
			} else {
				if (investigator == null || investigator.equalsIgnoreCase("")){
					JOptionPane.showMessageDialog(null,
							"You have to choose an existing investigator"+"\n"+"or create a new one by write the name in the invesitgator box",
							"Error:No Investigor",
							JOptionPane.ERROR_MESSAGE);
					dontChangePane = true;
				}
			}
			log.debug("adding the investigator " + investigator+"to the case");
			cm.setInvestigator(investigator);
		}
		
		log.debug("call to showAddEvidence. This is to show next screen");
		if (!dontChangePane){
			parent.showAddEvidence();
		}
	}
}